package com.imcrl.component.page
{
	import com.imcrl.event.TabEvent;
	
	import starling.display.DisplayObject;
	import starling.display.DisplayObjectContainer;
	import starling.events.Event;
	import starling.events.EventDispatcher;

	[Event(name="change", type="com.clayui.events.TabEvent")]
	
	/**
	 * Tab导航;
	 * @author crl;
	 * 
	 */	
	public class TabNavigator extends EventDispatcher
	{
		private var _selectItem:ITabItem;
		protected var _tabList:Array;
		
		private var _total:int;
		private var _container:DisplayObjectContainer;
		
		/**
		 * 
		 * @param container 项要加到的容器,如果项为共用,可不必传递此参数,也不必传递item项的target;
		 * 
		 */		
		public function TabNavigator(container:DisplayObjectContainer=null)
		{
			_tabList=new Array();
			
			_container=container;
		}
		
		/**
		 * 添加一tab项; 
		 * @param tabItem
		 * 
		 */		
		public function addTabItem(tabItem:ITabItem):Boolean{
			var index:int=_tabList.indexOf(tabItem);
			if(index !=-1){
				return false;	
			}
			
			var target:DisplayObject=tabItem.target;
			if(target){
				if(_container){
					if(target.parent)target.parent.removeChild(target);
				}else{
					target.visible=false;
				}
			}
			
			tabItem.index=_total;
			(tabItem as EventDispatcher).addEventListener(Event.TRIGGERED,triggeredHande);
			
			_total=_tabList.push(tabItem);
			return true;
		}
		
		/**
		 * 删除一个tab项; 
		 * @param tabItem
		 * @return  是否删除成功;
		 * 
		 */		
		public function removeTabItem(tabItem:ITabItem):Boolean{
			var index:int=_tabList.indexOf(tabItem);
			
			if(index==-1)return false;
			
			(tabItem as EventDispatcher).removeEventListener(Event.TRIGGERED,triggeredHande);
			if(tabItem==selectItem){
				selectItem=null;
			}
			
			_tabList.splice(index,1);
			
			_total--;
			
			//重排当前删除项后续项的索引;
			for(var i:int=index;i<_total;i++){
				tabItem=_tabList[index];
				tabItem.index=index;
			}
			
			return true;
		}
		
		/**
		 *清除所有的tab项; 
		 * 
		 */		
		public function clear():void{
			for each(var tabItem:ITabItem in _tabList){
				(tabItem as EventDispatcher).removeEventListener(Event.TRIGGERED,triggeredHande);
			}
			selectItem=null;
			
			_tabList.splice(0);
			_total=0;
		}
		
		protected function triggeredHande(event:Event):void{
			selectItem=event.currentTarget as ITabItem;
		}
		
		/**
		 * 索引某个tab; 
		 * @param index
		 * 
		 */		
		public function set selectIndex(index:int):void{
			var value:ITabItem=_tabList[index];
			if(value==null){
				return;
			}
			selectItem=value;
		}
		
		/**
		 * 取得当前选择项索引 
		 * @return 
		 * 
		 */		
		public function get selectIndex():int{
			if(selectItem){
				return selectItem.index;
			}
			return -1;
		}
		
		/**
		 * 选择特定的tab; 
		 * @param value
		 * 
		 */		
		public function set selectItem(value:ITabItem):void{
			
			if(_selectItem==value){
				//可能内容数据更新了,如果有设置将都抛出此事件;
				this.dispatchEvent(new TabEvent(TabEvent.CHANGE,_selectItem));
				return;
			}
			
			var oldItem:ITabItem=_selectItem;
			var target:DisplayObject;
			if(oldItem){
				target=oldItem.target;
				if(target){
					if(_container){
						_container.removeChild(target);
					}else{
						target.visible=false;
					}
				}
				oldItem.selected=false;
			}
			_selectItem=value;
			if(_selectItem){
				target=_selectItem.target;
				if(target){
					if(_container){
						_container.addChild(target);
					}else{
						target.visible=true;
					}
				}
				_selectItem.selected=true;
			}
			
			this.dispatchEvent(new TabEvent(TabEvent.CHANGE,_selectItem,oldItem));
		}
		
		/**
		 * 拥有数目; 
		 * @return 
		 * 
		 */		
		public function get length():int{
			return _total;
		}
		
		
		/**
		 * 取得相应索引Tab项; 
		 * @param index
		 * @return 
		 * 
		 */		
		public function getTabeItem(index:int):ITabItem{
			index=Math.max(0,Math.min(index,_total-1));
			return _tabList[index] as ITabItem;
			
		}
		
		/**
		 * 当前选择Tab项;
		 * @return 
		 * 
		 */		
		public function get selectItem():ITabItem{
			return this._selectItem;
		}
		
	}
}